import {SHOW_GLOBAL_ALERT,HIDE_GLOBAL_ALERT} from '@/store/actionType'
const TIMEOUT = 2000
let timer = null
export const globalAlertDispatch = (dispatch)=>{
  return {
    showGlobalAlert(msg='',type='success',timeout=TIMEOUT){
      dispatch({
        type:SHOW_GLOBAL_ALERT,
        value:{
          msg,type
        }
      })
      clearTimeout(timer)
      timer = setTimeout(() => {
        this.hideGlobalAlert()
      }, timeout);
    },
    hideGlobalAlert(){
      dispatch({type:HIDE_GLOBAL_ALERT})
    },
    showSuccessAlert(msg='',timeout){
      this.showGlobalAlert(msg,'success',timeout)
    },
    showErrorAlert(msg='',timeout){
      this.showGlobalAlert(msg,'error',timeout)
    },
    showInfoAlert(msg='',timeout){
      this.showGlobalAlert(msg,'info',timeout)
    },
    showWarningAlert(msg='',timeout){
      this.showGlobalAlert(msg,'warning',timeout)
    }
  }
}

export const loginExpire = (err,history,props)=>{
  if(err.response?.data?.errCode === 1002){
    props.showErrorAlert && props.showErrorAlert('登录失效')
    history.push('/login')
  }
}